#!/usr/bin/python
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Contains the objects required for RASE sniff resource access. """
try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree

import ratom
import rawsc
from rawsc import RAWS_NAMESPACE
from rawsc.rase import *

class SniffParams(rawsc.RawsParams):
    _children = {}
    _children['{%s}id' % RAWS_NAMESPACE] = ('id', Id)
    _children['{%s}type' % RAWS_NAMESPACE] = ('type', Type)
    _children['{%s}bytes' % RAWS_NAMESPACE] = ('bytes', Bytes)
    _children['{%s}sniffed_bytes' % RAWS_NAMESPACE] = ('sniffed_bytes', SniffedBytes)
    _children['{%s}src_ip' % RAWS_NAMESPACE] = ('src_ip', SrcIp)
    _children['{%s}tgt_ip' % RAWS_NAMESPACE] = ('tgt_ip', TgtIp)
    _children['{%s}src_port' % RAWS_NAMESPACE] = ('src_port', SrcPort)
    _children['{%s}tgt_port' % RAWS_NAMESPACE] = ('tgt_port', TgtPort)
    _children['{%s}protocol' % RAWS_NAMESPACE] = ('protocol', Protocol)
    _children['{%s}datetime' % RAWS_NAMESPACE] = ('datetime', Datetime)

    def __init__(self, id=None, type=None, bytes=None, sniffed_bytes=None, src_ip=None, tgt_ip=None, src_port = None, tgt_port = None, protocol = None, datetime = None):
        rawsc.RawsParams.__init__(self, extension_elements=None, extension_attributes=None)
        self.id = Id(text = id)
        self.type = Type(text = type)
        self.bytes = Bytes(text = bytes)
        self.sniffed_bytes = SniffedBytes(text = sniffed_bytes)
        self.src_ip = SrcIp(text = src_ip)
        self.tgt_ip = TgtIp(text = tgt_ip)
        self.src_port = SrcPort(text = src_port)
        self.tgt_port = TgtPort(text = tgt_port)
        self.protocol = Protocol(text = protocol)
        self.datetime = Datetime(text = datetime)

def SniffParamsFromString(xml_string):
    return ratom.CreateClassFromXMLString(SniffParams, xml_string)   


class SniffContent(rawsc.RawsContent):
    """The atom:content element"""
    _children = ratom.AtomBase._children.copy()
    _children['{%s}params' % (RAWS_NAMESPACE)] = ('params', SniffParams)
    
    params = None
    
    def __init__(self, params=None):
        rawsc.RawsContent.__init__(self, extension_elements=None, extension_attributes=None)
        self.params = Params()
        if params:
            self.params = params
    
def SniffContentFromString(xml_string):
    return ratom.CreateClassFromXMLString(SniffContent, xml_string)   
    

class SniffEntry(rawsc.RawsEntry):
    """Represents a RASE sniff profile."""
    _children = rawsc.RawsEntry._children.copy()
    _children['{%s}content' % ratom.ATOM_NAMESPACE] = ('content', SniffContent)

    def __init__(self, author=None, category=None, atom_id=None, link=None, published=None, title=None, updated=None, content = None, extension_elements=None, extension_attributes=None):
        rawsc.RawsEntry.__init__(self, author=author, category=category, atom_id=atom_id, link=link, published=published, title=title, updated=updated,
                                extension_elements=extension_elements, extension_attributes=extension_attributes)
        self.content = SniffContent()
        if content:
            self.content = content

def SniffEntryFromString(xml_string):
    return ratom.CreateClassFromXMLString(SniffEntry, xml_string)
    

class SniffFeed(rawsc.RawsFeed, rawsc.LinkFinder):
    """Represents a RASE  feed."""
    _children = rawsc.RawsFeed._children.copy()
    _children['{%s}entry' % ratom.ATOM_NAMESPACE] = ('entry', [SniffEntry])

def SniffFeedFromString(xml_string):
  return ratom.CreateClassFromXMLString(SniffFeed, xml_string)

